package com.jshare.service.Impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.jshare.dao.DetoolDAO;
import com.jshare.entity.Detool;
import com.jshare.service.DetoolService;

@Component("detoolService")
public class DetoolServiceImpl implements DetoolService{
	
	private DetoolDAO detoolDao;
	private int rowCount = 0;
	private int pageCount = 0;
	
	@Override
	public void update(Detool detool) {
		this.detoolDao.merge(detool);
	}
	public DetoolDAO getDetoolDao() {
		return detoolDao;
	}

	@Resource
	public void setDetoolDao(DetoolDAO detoolDao) {
		this.detoolDao = detoolDao;
	}
	
	@Override
	public Detool loadById(int id) {
		return this.detoolDao.findById(id);
	}
	
	@Override
	public int getPageCount() {
		return pageCount;
	}

	@Override
	public int getRowCount() {
		return rowCount;
	}
	
	@Override
	public List<Detool> getListByFuzzyName(String name) {
		
		List<Detool> detools = this.detoolDao.getListByFuzzyName(name);
		
		rowCount = detools.size();
		
		return detools;
		
	}
	
	@Override
	public List<Detool> getListByPage(int pageNow,int pageSize) {
		rowCount = this.detoolDao.findAll().size();
		if(rowCount % pageSize == 0) {
			pageCount = rowCount / pageSize;
		} else {
			pageCount = rowCount / pageSize + 1;
		}
		return this.detoolDao.getListForPage((pageNow-1)*pageSize, pageSize);
	}
	
	@Override
	public boolean exists(Detool detool) {
		boolean check = false;
		List<Detool> detools = this.detoolDao.findAll();
		for(Detool temp : detools) {
			if(detool.getDwUrl().equals(temp.getDwUrl())) {
				check = true;
			}
		}
		return check;
	}
	
	@Override
	public void download(int id) {
		Detool detool = this.detoolDao.findById(id);
		detool.setDownloadcount(detool.getDownloadcount()+1);
System.out.println("detool.getDownloadcount()"+detool.getDownloadcount());		
		this.detoolDao.merge(detool);
	}
	
	@Override
	public boolean delete(int id, String savePath) {
		String filePath = ServletActionContext.getServletContext().
		getRealPath(savePath) + "/" 
		+ this.detoolDao.findById(id).getDwUrl();
		
		System.out.println("filePath="+filePath);
		this.deleteFile(new File(filePath));
		this.detoolDao.delete(this.detoolDao.findById(id));
		return true;
	}
	
	//******************删除文件***********************
	public boolean deleteFile(File file){     

        if(file.isFile() && file.exists()){     
            file.delete();     
            System.out.println("删除文件成功！");     
            return true;
        }else{     
            System.out.println("删除文件失败！");     
            return false;     
        }
    }
	
	@Override
	public void add(Detool detool,File upload,String savePath,String uploadFileName) {
		detool.setUploadtime(new Date());
		this.uploadService(upload, savePath, uploadFileName);
		this.detoolDao.attachDirty(detool);
	}
	
	//********************上传文件************************
	public boolean uploadService(File upload,String savePath,String uploadFileName){
		boolean b = false;
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath(savePath)+"\\"+uploadFileName);
			//System.out.println(savePath);
			fis = new FileInputStream(upload);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0){
				fos.write(buffer,0,len);
			}
			
			b = true;
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return b;
	}
	
}
